﻿app.factory('signalrMonitoringSvc', function ($, $rootScope) {

    var initialize = function () {

        $.connection.hub.logging = true;
        $.connection.hub.start()
            .done(function () {
            console.log('SignalR connection id: ' + $.connection.id);
            $.connection.monitoringHub.server.startTimer();
            })

        $.connection.hub.error(function (err) {
            console.log('SignalR error occurred: ' + err);
        });

        //Publishing an event when server pushes a counters message
        $.connection.monitoringHub.on('updateCounters', function (message) {
            $rootScope.instances = message.instances;
            $rootScope.$apply();
        });
    };

    return {
        initialize: initialize,
    };
});